import http from '../../apis/api.js';
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    repairType: '',
    type: '',
    allMaterialParts: [],
    formData: {
      wechatMaterialName: '',
      parts: '',
      batch: '',
      code:''
    },
    error: {
      wechatMaterialName: '',
    },
    submitDisable: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    console.log(options);
    self.data.id = options.id;
    self.data.repairType = options.repairType;
    wx.setNavigationBarTitle({
      title: '新增换下的部件'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**获取所有部件类型*/
  getAllMaterialParts(data) {
    http.jqbzAll(data).then(res => {
      self.data.allMaterialParts = res.content;
      self.setData({
        allMaterialParts: self.data.allMaterialParts
      })
    })
  },
  selectItem(e) {
    let selectContent = e.detail;
    self.data.formData.wechatMaterialName = selectContent.name;
    self.data.formData.parts = selectContent.id;

    self.setData({
      formData: self.data.formData
    })

  },
  getSearchAll(e) {
    self.getAllMaterialParts({
      keywords: e.detail
    })
  },
  /**获取input框的值*/
  getValus(e) {
    let value = e.detail.value
    switch (e.currentTarget.dataset.type) {
      case 'batch':
        self.data.formData.batch = value;
        break;
      case 'code':
        self.data.formData.code = value;
        break;
    }
    self.setData({
      formData: self.data.formData
    })
  },
  checked(name) {
    /**表单输入验证 */
    switch (name) {
      case 'wechatMaterialName':
        if (self.data.formData.wechatMaterialName === '') {
          self.data.error.wechatMaterialName = "备件或零件不能为空"
          return false
        }
        self.data.error.wechatMaterialName = '';
        return true;
        break;
    }
  },
  /**点击提交*/
  submitInfo() {
    self.setData({
      submitDisable: true
    })
    if (self.checked('wechatMaterialName')) {
      let postData = {
        pid: self.data.id,
        batch: self.data.formData.batch,
        code: self.data.formData.code,
        parts: self.data.formData.parts
      };
      switch (self.data.repairType){
        case 'self':
          postData.type=1;
        break;
        case 'send':
          postData.type = 2;
        break;
        case 'maintain':
          postData.type = 3;
        break
      }
      http.insertUninstallMaterial(postData).then(res => {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
            success() {
              wx.navigateBack();
            },
            complete() {
              self.setData({
                submitDisable: false
              })
            }
          })
        }, error => {
          self.setData({
            submitDisable: false
          })
        })
    } else {
      self.setData({
        error: self.data.error,
        submitDisable: false
      })
    }
  },
})